package com.zq.it.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class Office2Swf 
{	
	/**
	 * synchronized
	 */
	
	private static File sourceFile; // 转换源文件
	private static File pdfFile; // PDF目标文件
	private static File swfFile; // SWF目标文件
	private static String  openoffice_home="C:/Program Files/OpenOffice4.0/";

	private static Runtime r;

	public static synchronized void ConvertSwf(String toolUrl, String readUrl,
			String file, String fileName) throws ConnectException {

		sourceFile = new File(readUrl + "/courseWare/readOnline/" + file);
		pdfFile = new File(readUrl + "/courseWare/readOnline/"+fileName+".pdf");
		swfFile = new File(readUrl + "/courseWare/readOnline/" + fileName+".swf"); 
		
		System.out.println(sourceFile);
		System.out.println(pdfFile);
		System.out.println(swfFile);
		
		/**Process p = r.exec("\"" + toolUrl
		+ "f2swf.exe\" " + "\""
		+ pdfFile.getPath() + "\"" + " -o " + "\""
		+ swfFile.getPath() + "\" -T 9");
*/
		
		if (sourceFile.exists()) {
			if (!pdfFile.exists()) {
				OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
				try {
					office2Pdf(connection);
				} catch (java.net.ConnectException e)			
				{
					System.out.println("OpenOffice服务未启动,尝试启动..."); 
					Process pro = null;	 
					// 启动OpenOffice的服务	 
					//String command = openoffice_home+"\"soffice\" -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizar";	 
					String command = openoffice_home+"program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\""; 
					try {
						pro = Runtime.getRuntime().exec(command);
						
						Thread.sleep(3000);
						System.out.println("服务启动成功！");
						office2Pdf(connection);
						
					} catch (Exception e1){
						System.out.println("服务启动失败！");
						throw e;
					}finally      /*****warming*******/
					{
						pro.destroy();
					}
				} 
				catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) 				
				{	
					e.printStackTrace();
					System.out.println("读取文件失败");
					throw e;
				} catch (Exception e) {
					e.printStackTrace();
					try {
						throw e;
					} catch (Exception e1) {
						e1.printStackTrace();
				 }
				}
			} else {
				System.out.println("已转换为PDF，无需再次转换");
			}
		} else {
			System.out.println("要转换的文件不存在");
		}
		
		// 转换成swf文件
	
		r = Runtime.getRuntime();
	
		if (!swfFile.exists()) 
		{
			if (pdfFile.exists()) {
				try {
					String s="";
					Process p = r.exec( toolUrl+ "/swfTool/pdf2swf.exe "+ pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");

					 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
			         while ((s=bufferedReader.readLine())!= null){System.out.println(s);};  
					p.waitFor();
					swfFile.createNewFile();
					System.out.println("第三步：转换为SWF格式	路径：" + swfFile.getPath());
					System.out.println("第四步：转换为SWF格式   名称："+ swfFile.getName());
					if (pdfFile.exists()) {
						pdfFile.delete();
					}
				} catch (Exception e) {
					e.printStackTrace();
					try {
						throw e;
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			} else {
				System.out.println("PDF文件不存在，无法转换");
			}
		} else {
			System.out.println("已经转为SWF文件，无需再次转换");
		}
	}
	
	private static void office2Pdf(OpenOfficeConnection connection) throws IOException{
		connection.connect();
		DocumentConverter converter = new OpenOfficeDocumentConverter(
				connection);
		converter.convert(sourceFile, pdfFile);
		pdfFile.createNewFile();
		connection.disconnect();
		System.out.println("第二步：转换为PDF格式	路径" + pdfFile.getPath());
	}
}
